/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plan;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import main.Main;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author tanselkacamak
 */
public class StundenplanMain extends javax.swing.JFrame {

    /**
     * Creates new form StundenplanMain
     */
    String[] titel = new String[]{"Uhrzeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"};
    private Main m;

    public StundenplanMain() throws ParserConfigurationException, SAXException, IOException {
        initComponents();
        uhrzeiten();
        doubleclick();
        einlesen();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        planTabelle = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        planTabelle.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Uhrzeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        planTabelle.setToolTipText("");
        planTabelle.getTableHeader().setResizingAllowed(false);
        planTabelle.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(planTabelle);
        planTabelle.getColumnModel().getColumn(0).setResizable(false);
        planTabelle.getColumnModel().getColumn(1).setResizable(false);
        planTabelle.getColumnModel().getColumn(2).setResizable(false);
        planTabelle.getColumnModel().getColumn(3).setResizable(false);
        planTabelle.getColumnModel().getColumn(4).setResizable(false);
        planTabelle.getColumnModel().getColumn(5).setResizable(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(0, 0, 0)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 391, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 120, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(71, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    private void einlesen() throws ParserConfigurationException, SAXException, IOException {
        File xml = new File("stundenplan.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

        Document doc = dBuilder.parse(xml);

        Element docE = doc.getDocumentElement();

        //Anzeige von der Information
        //Element plan1 = (Element) docE.getElementsByTagName("Block").item(2).getChildNodes().item(3);
        //Anzeige von dem fach

        Element plan;
        /*for (int j = 1; j <= 5; j++) {
            for (int i = 0; i <= 5; i++) {

                plan = (Element) docE.getElementsByTagName("Block").item(i).getChildNodes().item(1);
                planTabelle.setValueAt(plan.getTextContent(), i, j);

            }
        }*/
        plan = (Element) docE.getElementsByTagName("Block").item(1).getChildNodes().item(1);
        //plan = (Element) docE.getChildNodes().item(2);
        System.out.println(plan.getTextContent());
               // planTabelle.setValueAt(plan.getTextContent(), 2, 1);

        // System.out.println("Root element :" + doc.getDocumentElement().getNodeName());





    }

    private void doubleclick() {
        planTabelle.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                planTabelle = (JTable) e.getSource();
                if (e.getClickCount() == 2) {
                    int clickedRow = planTabelle.getSelectedRow();
                    int clickedColumn = planTabelle.getSelectedColumn();
                    System.out.print("helpme");
                    if (clickedRow != -1 && clickedColumn == 1) {
                        String temp = planTabelle.getValueAt(clickedRow, clickedColumn) + "";
                        System.out.print("helpme2");
                        if (temp.equals("")) {
                            System.out.print("leer");
                            m.oeffneNeuerEintrag();
                        } else {
                            System.out.print("voll");
                        }
                    }
                }
            }
        });
    }

    //Für das Erzeugen der Vorlesungsuhrzeiten in der Tabelle
    private void uhrzeiten() {
        planTabelle.setValueAt("08:00 - 09:30", 0, 0);
        planTabelle.setValueAt("09:45 - 11:15", 1, 0);
        planTabelle.setValueAt("11:30 - 13:00", 2, 0);
        planTabelle.setValueAt("13:30 - 15:00", 3, 0);
        planTabelle.setValueAt("15:15 - 16:45", 4, 0);
        planTabelle.setValueAt("17:00 - 18:30", 5, 0);
    }

    public class myTableModel extends DefaultTableModel {

        myTableModel() {
            super(titel, 7);
        }

        @Override
        public boolean isCellEditable(int row, int cols) {
            return false;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable planTabelle;
    // End of variables declaration//GEN-END:variables
}
